package com.heima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.common.R;
import com.heima.reggie.pojo.Category;
import com.heima.reggie.pojo.Dish;
import com.heima.reggie.pojo.DishDto;
import com.heima.reggie.pojo.DishFlavor;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.DishFlavorService;
import com.heima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j

public class DishController {
@Autowired
    public DishService service;
@Autowired
public CategoryService categoryService;
@Autowired
public DishFlavorService dishFlavorService;
    @GetMapping("/page")
    public R page123123(Integer page,Integer pageSize,String name){
        Page<Dish> page1 = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        LambdaQueryWrapper<Dish>  lam = new LambdaQueryWrapper<>();
        lam.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        lam.orderByDesc(Dish::getName).orderByDesc(Dish::getUpdateTime);
        service.page(page1,lam);

        BeanUtils.copyProperties(page1,dishDtoPage,"records");
        List<Dish> records = page1.getRecords();
        List<DishDto> list = records.stream().map((item) -> {

            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);


            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);

        return R.success(dishDtoPage);


    }

@PostMapping
public R save(@RequestBody  DishDto dishDto){
        service.saveWithFlavor(dishDto);

return R.success("新增成功");




}


    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = service.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        service.updateWithFlavor(dishDto);
        return R.success("修改菜品成功");
    }

@GetMapping("/list")
    public R<List> categoryid(Dish dish){
        //根据菜品分类id还有启用状态查询
    LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
    lambdaQueryWrapper.like(dish.getName()!=null,Dish::getName,dish.getName());
    lambdaQueryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

List<Dish> list = service.list(lambdaQueryWrapper);
    List<DishDto> collect = list.stream().map((lis) -> {
        DishDto dishDto = new DishDto();

        LambdaQueryWrapper<DishFlavor> df = new LambdaQueryWrapper<>();
        //获取当前菜品id获取口味
        df.eq(lis.getId() != null, DishFlavor::getDishId, lis.getId());
        List<DishFlavor> list1 = dishFlavorService.list(df);
        dishDto.setFlavors(list1);

        BeanUtils.copyProperties(lis, dishDto);

        return dishDto;
    }).collect(Collectors.toList());


    return R.success(collect);
    }



}
